<?php

/**
 * AceEditor class file.
 *
 * @author Martin Gelder <darkspotinthecorner {at} gmail {dot} com>
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 * @version 1.0
 */

class AceEditorWidget extends CWidget {

	public $options      = array();
	public $htmlOptions  = array();
	public $valid_themes = array('clouds', 'clouds_midnight', 'cobalt', 'crimson_editor', 'dawn', 'eclipse', 'idle_fingers', 'kr_theme', 'merbivore', 'merbivore_soft', 'mono_industrial', 'monokai', 'pastel_on_dark', 'solarized_dark', 'solarized_light', 'textmate', 'twilight', 'vibrant_ink');
	public $valid_modes  = array('c_cpp', 'clojure', 'coffee', 'csharp', 'css', 'groovy', 'html', 'java', 'javascript', 'json', 'ocaml', 'perl', 'php', 'python', 'ruby', 'scad', 'scala', 'scss', 'svg', 'textile', 'xml');
	
	public function run()
	{
		$id = $this->getId();

		// merge options with default values
		$defaultOptions = array(
			'content' => '',
			'layout' => array(
				'height' => '400px',
				'width'  => '500px',
			),
			'theme' => 'clouds',
			'mode'  => '',
		);

		$this->options = CMap::mergeArray(
			$defaultOptions,
			$this->options
		);
		
		$this->validateTheme();
		$this->validateMode();
		
		$this->htmlOptions['id'] = $id;
		
		echo(CHtml::openTag('div', $this->htmlOptions));
		echo($this->options['content']);
		echo(CHtml::closeTag('div'));
		
		$jsOptions = CJavaScript::encode($this->options);
		$this->registerScripts($id);
	}
	
	protected function validateTheme()
	{
		$theme = strtolower($this->options['theme']);
		if(!in_array($theme, $this->valid_themes))
		{
			$this->options['theme'] = 'clouds';
		}
	}
	
	protected function validateMode()
	{
		$mode = strtolower($this->options['mode']);
		if(!empty($mode))
		{
			if(!in_array($mode, $this->valid_modes))
			{
				$this->options['mode'] = '';
			}
		}
	}
	
	protected function registerScripts($id)
	{
		$absid    = __CLASS__ . '#' . $id;
		$basePath = dirname(__FILE__) . '/assets/';
		$baseUrl  = Yii::app()->getAssetManager()->publish($basePath);

		$cs = Yii::app()->clientScript;
		
		$cs->registerCoreScript('jquery');
		$cs->registerCoreScript('jquery.ui');
		
		$cs->registerScriptFile(($baseUrl . '/js/ace.js'), CClientScript::POS_HEAD);
		$cs->registerScriptFile(($baseUrl . '/js/theme-' . strtolower($this->options['theme']) . '.js'), CClientScript::POS_HEAD);
		if(!empty($this->options['mode']))
		{
			$cs->registerScriptFile(($baseUrl . '/js/mode-' . strtolower($this->options['mode']) . '.js'), CClientScript::POS_HEAD);
		}
		
		$escript  = '';
		$escript .= 'var editor = ace.edit("' . $id . '");';
		$escript .= 'editor.setTheme("ace/theme/' . strtolower($this->options['theme']) . '");';
		if(!empty($this->options['mode']))
		{
			$escript .= 'var editorMode = require("ace/mode/' . strtolower($this->options['mode']) . '").Mode;';
			$escript .= 'editor.getSession().setMode(new editorMode());';
		}
		
		$cs->registerScript($absid, $escript, CClientScript::POS_LOAD);
		
		$cs->registerCss($absid, '#' . $id . ' { position:relative; height:' . $this->options['layout']['height'] . '; width:' . $this->options['layout']['width'] . '; }');
	}
}